package com.example.demo.common.api;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import lombok.Data;
import org.springframework.util.ObjectUtils;

/**
 * @author debj
 * @version 1.0
 * @date 2021/11/18 17:20
 */
@Data
public class IotApi {

    /**
     * 龙池 appId
     */
    private static final String APP_ID = "iot_6c7394b660a04bb09721a6a5f04e81fe";
    /**
     * 传感器平台 1.0 URL
     */
    private static final String API_URL = "http://47.101.158.124:10000/api/1.0/";

    private IotApi() {
    }

    /**
     * 获取所有主控设备列表
     *
     * @return 列表
     */
    public static JSONArray getMainDevices() {
        return getMainDevices(APP_ID);
    }

    /**
     * 获取所有主控设备列表
     *
     * @return 列表
     */
    public static JSONArray getMainDevices(String appId) {
        try {
            HttpRequest post = HttpUtil.createPost(API_URL + "/api/1.0/device/devicelist");
            post.header("appId", appId);
            HttpResponse execute = post.execute();
            if (execute.getStatus() == HttpStatus.HTTP_OK) {
                JSONObject jsonObject = new JSONObject(execute.body());
                if (jsonObject.getInt("code").equals(10000)) {
                    return jsonObject.getJSONArray("data");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JSONArray();
    }


    /**
     * 传感器实时数据
     *
     * @param deviceId 设备 id
     * @return jsonArray || .new
     */
    public static JSONObject realTime(String deviceId) {
        return realTime(APP_ID, deviceId);
    }


    /**
     * 传感器实时数据
     *
     * @param appid    appid
     * @param deviceId 设备 id
     * @return jsonArray || .new
     */
    public static JSONObject realTime(String appid, String deviceId) {
        try {
            HttpRequest post = HttpUtil.createPost(API_URL + "device/list/" + deviceId);
            post.header("appId", appid);
            HttpResponse execute = post.execute();
            if (execute.getStatus() == HttpStatus.HTTP_OK) {
                JSONObject jsonObject = new JSONObject(execute.body());
                if (jsonObject.getInt("code").equals(10000)) {
                    return jsonObject.getJSONObject("data");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JSONObject();
    }


    /**
     * 一级主控下的传感器实时数据
     *
     * @param deviceId 设备 id
     * @return jsonArray || .new
     */
    public static JSONArray realTimeWithSon(String deviceId) {
        return realTimeWithSon(APP_ID, deviceId);
    }

    /**
     * 一级主控下的传感器实时数据
     *
     * @param appid    appid
     * @param deviceId 设备 id
     * @return jsonArray || .new
     */
    public static JSONArray realTimeWithSon(String appid, String deviceId) {
        try {
            HttpRequest post = HttpUtil.createPost(API_URL + "device/list/" + deviceId);
            post.header("appId", appid);
            HttpResponse execute = post.execute();
            if (execute.getStatus() == HttpStatus.HTTP_OK) {
                JSONObject jsonObject = new JSONObject(execute.body());
                if (jsonObject.getInt("code").equals(10000)) {
                    return jsonObject.getJSONObject("data").getJSONArray("sensors");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JSONArray();
    }


    /**
     * 自身传感器实时数据
     *
     * @param deviceId 设备 id
     * @return jsonObject || .new
     */
    public static JSONObject realTimeWithSelf(String deviceId) {
        return realTimeWithSelf(APP_ID, deviceId);
    }


    /**
     * 自身传感器实时数据
     *
     * @param appid    appid
     * @param deviceId 设备 id
     * @return jsonObject || .new
     */
    public static JSONObject realTimeWithSelf(String appid, String deviceId) {
        try {
            HttpRequest post = HttpUtil.createPost(API_URL + "/device/list/" + deviceId);
            if (ObjectUtils.isEmpty(appid)){
                post.header("appId", APP_ID);
            }else {
                post.header("appId", appid);
            }
            HttpResponse execute = post.execute();
            if (execute.getStatus() == HttpStatus.HTTP_OK) {
                JSONObject jsonObject = new JSONObject(execute.body());
                if (jsonObject.getInt("code").equals(10000)) {
                    return jsonObject.getJSONObject("data").getJSONObject("device");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JSONObject();
    }


    /**
     * 开关设备
     *
     * @param deviceId     设备 id
     * @param controlValue 0 关闭、1 打开
     */
    public JSONObject control(String deviceId, Integer controlValue) {
        return control(APP_ID, deviceId, controlValue);
    }


    /**
     * 开关设备
     *
     * @param appid        appid
     * @param deviceId     设备 id
     * @param controlValue 0 关闭、1 打开
     */
    public static JSONObject control(String appid, String deviceId, Integer controlValue) {
        try {
            HttpRequest post = HttpUtil.createPost(API_URL + "/control/" + deviceId + "/" + controlValue);
            post.header("appId", appid);
            HttpResponse execute = post.execute();
            if (execute.getStatus() == HttpStatus.HTTP_OK) {
                JSONObject jsonObject = new JSONObject(execute.body());
                if (jsonObject.getInt("code").equals(10000)) {
                    return jsonObject.getJSONObject("data");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JSONObject();
    }

}